package com.youi.service.impl;

import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.dao.OrdergoodsDao;
import com.youi.entity.Ordergoods;
import com.youi.entity.Userorder;
import com.youi.entity.Yibaopayment;
import com.youi.service.OrdergoodsService;
import com.youi.service.UserorderService;
import com.youi.service.YibaopaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrdergoodsServiceImpl extends BaseServiceImpl<Ordergoods> implements OrdergoodsService{
		
		private OrdergoodsDao ordergoodsDao;
		@Autowired
		private UserorderService userorderService;
		@Autowired
		private YibaopaymentService yibaopaymentService;
		
		@Autowired
		public void setfaultRecordDao(OrdergoodsDao ordergoodsDao){
			this.ordergoodsDao=ordergoodsDao;
			super.setBaseDAO(ordergoodsDao);
		}

		@Override
		public List<String> getGoodsIdList(String pms) {
			// TODO Auto-generated method stub
			List<Long> orderIdList=null;
			List<String> orderNoList=null;
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> searchParam=new HashMap<String, Object>();
			searchParam.put("name", pms);
			searchParam.put("goodsNo", pms);
			List<Ordergoods> ordergoodsList=ordergoodsDao.getByParams(params, searchParam, null);
			if (ordergoodsList!=null&&ordergoodsList.size()>0) {
				orderIdList=new ArrayList<Long>();
				for (Ordergoods ordergoods : ordergoodsList) {
					orderIdList.add(ordergoods.getOrderId());
				}
				orderNoList=new ArrayList<>();
				params=new HashMap<String, Object>();
				params.put(Common.DELSTATUS, DelStatus.NODEL);
				params.put(Common.ID, orderIdList);
				List<Userorder> userorderList=userorderService.getByParams(params);
				if (userorderList!=null&&userorderList.size()>0) {
					for (Userorder userorder : userorderList) {
						orderNoList.add(userorder.getOrderNo());
					}
				}
			}
			
			return orderNoList;
		}

		/**
		 * 查询订单id 根据商品编号
		 */
		@Override
		public List<Long> getOrderIds(String goodsNo) {
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put("goodsNo", goodsNo);
			List<Ordergoods> ordergoods=  ordergoodsDao.getObjectList(params);
			if (ordergoods!=null&&ordergoods.size()>0) {
				List<Long> ids=new ArrayList<Long>();
				for (Ordergoods ordergood: ordergoods) {
					ids.add(ordergood.getOrderId());
				}
				return ids;
			}
			return null;
		}

		@Override
		public Userorder getUserorder(Ordergoods ordergoods) {
			// TODO Auto-generated method stub
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, ordergoods.getOrderId());
			Userorder userorder=userorderService.getByParam(params);
			if(userorder!=null) {
				params=new HashMap<String, Object>();
				params.put(Common.DELSTATUS, DelStatus.NODEL);
				Map<String, Object> searchParams=new HashMap<String, Object>();
				searchParams.put("orderNos", userorder.getOrderNo());
				Yibaopayment yibaopayment=yibaopaymentService.getByParam(params,searchParams,null);
				if (yibaopayment!=null) {
					userorder.setTopOrderNo(yibaopayment.getSerialNumber());
				}
				return userorder;
			}
			return null;
		}

	@Override
	public List<Ordergoods> listByOrderId(Long orderId) {
		return ordergoodsDao.listByOrderId(orderId);
	}
}